#include "confighandler.h"

ConfigHandler::ConfigHandler()
{
    m_Data = new QGSettings(KYLINSCREENCAP);
    m_usdMediaKeys = new QGSettings(GSETTINGS_USD_MEDIA_KEYS_SCHEMA);
}

bool ConfigHandler::getCameraRecord()
{
    return m_Data->get("record-camera").toBool();
}

int ConfigHandler::getCurrentCamera()
{
    return m_Data->get("current-using-camera").toInt();
}

QString ConfigHandler::getSavePath()
{
    QString path = m_Data->get("save-path").toString();
    return path;
}

SaveFormat ConfigHandler::getSaveFormat()
{
    int index = m_Data->get("save-format").toInt();
    return static_cast<SaveFormat>(index);
}

SaveResolution ConfigHandler::getResolution()
{
    int index = m_Data->get("resolution").toInt();
    return static_cast<SaveResolution>(index);
}

SaveFrameRate ConfigHandler::getFrameRate()
{
    int index = m_Data->get("frame-rate").toInt();
    return static_cast<SaveFrameRate>(index);
}

QString ConfigHandler::getMicrophone()
{
    QString  microphone = m_Data->get("microphone").toString();
    return microphone;
}

SaveAction ConfigHandler::getCompletionAction()
{
    int index = m_Data->get("completion-action").toInt();
    return static_cast<SaveAction>(index);
}

void ConfigHandler::setCameraRecord(bool iscameraRecord)
{
    m_Data->set("record-camera", iscameraRecord);
}

void ConfigHandler::setCurrentCamera(int index)
{
    m_Data->set("current-using-camera", index);
}

void ConfigHandler::setSavePath(QString savePath)
{
    m_Data->set("save-path", savePath);
}

void ConfigHandler::setSaveFormat(SaveFormat saveformat)
{
    m_Data->set("save-format", static_cast<int>(saveformat) );
}

void ConfigHandler::setResolution(SaveResolution resolution)
{
    m_Data->set("resolution", static_cast<int>(resolution));
}

void ConfigHandler::setFrameRate(SaveFrameRate frameRate)
{
    m_Data->set("frame-rate", static_cast<int>(frameRate));
}

void ConfigHandler::setMicrophone(QString microphone)
{
    m_Data->set("microphone", microphone);
}

void ConfigHandler::setCompletionAction(SaveAction action)
{
    m_Data->set("completion-action", static_cast<int>(action));
}

QString ConfigHandler::getInvokeScreencap()
{
    QString shortchut = m_Data->get("invoke-screencap").toString();
    return shortchut;
}

QString ConfigHandler::getStartRecording()
{
    QString shortchut = m_Data->get("start-recording").toString();
    return shortchut;
}

QString ConfigHandler::getWindowRecording()
{
    QString shortchut = m_Data->get("window-recording").toString();
    return shortchut;
}

QString ConfigHandler::getFullScreenRecording()
{
    QString shortchut = m_Data->get("full-screen-recording").toString();
    return shortchut;
}

QString ConfigHandler::getPauseRecording()
{
    QString shortchut = m_Data->get("pause-recording").toString();
    return shortchut;
}

QString ConfigHandler::getOpenMicrophone()
{
    QString shortchut = m_Data->get("open-microphone").toString();
    return shortchut;
}

void ConfigHandler::setInvokeScreencap(QString shortchut)
{
    m_Data->set("invoke-screencap", shortchut);
}

void ConfigHandler::setStartRecording(QString shortchut)
{
    m_Data->set("start-recording", shortchut);
}

void ConfigHandler::setWindowRecording(QString shortchut)
{
    m_Data->set("window-recording", shortchut);
}

void ConfigHandler::setFullScreenRecording(QString shortchut)
{
    m_Data->set("full-screen-recording", shortchut);
}

void ConfigHandler::setPauseRecording(QString shortchut)
{
    m_Data->set("pause-recording", shortchut);
}

void ConfigHandler::setOpenMicrophone(QString shortchut)
{
    m_Data->set("open-microphone", shortchut);
}

void ConfigHandler::setShortcut(ShortcutRec shortcut, QString value)
{
   QString key = "";
    switch (shortcut) {
    case ShortcutRec::Invoke:
        key = "kylin-screencap";
        break;
    case ShortcutRec::Start:
        key = "start-recording";
        break;
    case ShortcutRec::Pause:
        key = "pause-recording";
        break;
    case ShortcutRec::Full:
        key = "full-screen-recording";
        break;
    case ShortcutRec::Win:
        key = "window-recording";
        break;
    case ShortcutRec::OpenMic:
        key = "open-microphone";
        break;
    default:
        break;
    }

    if(shortcut == ShortcutRec::Invoke){
        if(isHaveSystemShortcut()){
            QString value1 = keyToLib(value);
            value1.replace("Meta","Win");
            if(m_usdMediaKeys->get(key).toString() != value1){
                qDebug()<<"系统快捷键唤醒录屏 - 写入快捷键 =  "<< value1;
                m_usdMediaKeys->set(key,value1);
            }
        }
        key = "invoke-screencap";
        m_Data->set(key,value);
    }else{
        m_Data->set(key,value);
    }
}

QString ConfigHandler::getShortcut(ShortcutRec shortcut)
{
    QString key = "";
    switch (shortcut) {
    case ShortcutRec::Invoke:
        key = "kylin-screencap";
        break;
    case ShortcutRec::Start:
        key = "start-recording";
        break;
    case ShortcutRec::Pause:
        key = "pause-recording";
        break;
    case ShortcutRec::Full:
        key = "full-screen-recording";
        break;
    case ShortcutRec::Win:
        key = "window-recording";
        break;
    case ShortcutRec::OpenMic:
        key = "open-microphone";
        break;
    default:
        break;
    }

    if(shortcut == ShortcutRec::Invoke){
        if(isHaveSystemShortcut()){
            QString value = m_usdMediaKeys->get(key).toString().
                    replace("<","").
                    replace(">","+").
                    replace("Win","Meta");
            value = QKeySequence(value).toString();
            qDebug()<<"系统快捷键-唤醒录屏 - 读入快捷键 = "<< value;
            return value;
        }
        else{
//            key = "invoke-screencap";
//            QString value = m_Data->get(key).toString();
//            qDebug()<<"全局快捷键-唤醒录屏 - 读入快捷键 = "<< value;
            return "";
        }

    }else{
        return m_Data->get(key).toString();
    }
}

bool ConfigHandler::getKylinscreencap()
{
    bool enable = m_Data->get("screencap").toBool();
    return enable;
}

bool ConfigHandler::getCloseAfterScreencap()
{
    bool enable = m_Data->get("close-after-screencap").toBool();
    return enable;
}

bool ConfigHandler::isHaveSystemShortcut()
{
    QStringList  keyList = m_usdMediaKeys->keys();
    if(keyList.contains("kylinScreencap")){
        qDebug()<<"有系统快捷键";
        return true;
    }
    qDebug()<<"没有系统快捷键";
    return false;
}

QString ConfigHandler::keyToLib(QString key)
{
    if (key.contains("+")) {
        QStringList keys = key.split("+");
        if (keys.count() == 2) {
            QString lower = keys.at(1);
            QString keyToLib = "<" + keys.at(0) + ">" + lower.toLower();

            return keyToLib;
        } else if (keys.count() == 3) {
            QString lower = keys.at(2);
            QString keyToLib = "<" + keys.at(0) + ">" + "<" + keys.at(1) + ">" + lower.toLower();

            return keyToLib;
        } else if (keys.count() == 4) {
            QString lower = keys.at(3);
            QString keyToLib = "<" + keys.at(0) + ">" + "<" + keys.at(1) + ">" + "<" + keys.at(2) + ">" + lower.toLower();

            return keyToLib;
        }
    }
    return key;
}
